package com.cgw.room.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorSpace;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Shader;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.MotionEvent;

import com.cgw.room.DogControllerActivity;
import com.cgw.room.MainApp;
import com.cgw.room.R;
import com.cgw.room.util.BitmapUtil;

import java.util.Timer;
import java.util.TimerTask;

public class RockerSafeView extends View {
    int Radius = 150;
    int RadiusInner = 100;
    int rockerMode = -1;
    private static final int ACCELERATION_RANK = 5;
    private int mCurrentRank = 1;

    DogControllerActivity mActivity = null;

    private String[] ButtonNames = new String[4];

    RockerSafeListener mListener = null;
    private boolean mPressed = false;
    private long mPressedTimestamp = 0;
    private Point mPressedPoint = new Point(0, 0);


    public RockerSafeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mActivity = (DogControllerActivity)context;
        ButtonNames[0] = "";
        ButtonNames[1] = "";
        ButtonNames[2] = "";
        ButtonNames[3] = "";

        mListener = new RockerSafeListener() {
            @Override
            public void onRockerClick(int position, boolean isDown) {
                Log.i("[Test]","onRockerClick : " + position + "  " + isDown);
            }

            @Override
            public void toggleLeftOrRight(boolean isShow) {
                Log.i("[Test]","toggleLeftOrRight");
            }
        };
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Radius = this.getWidth() / 2;
        RadiusInner = Radius / 2;

        float[] angleStart = new float[4];
        float angleOffset = 0.0f;
        angleStart[0] = 45 + angleOffset;
        angleStart[1] = 135 + angleOffset;
        angleStart[2] = 225 + angleOffset;
        angleStart[3] = 315 + angleOffset;
        float section = 90;

        int colorBack = Color.parseColor("#484848");
        Paint paintBack = new Paint();
        paintBack.setColor(colorBack);

        int colorButton = Color.parseColor("#555555");
        Paint paintButton = new Paint();
        paintButton.setColor(colorButton);

        int ButtoSelected = Color.parseColor("#1AB6FF");
        Paint paintSelected = new Paint();
        paintSelected.setColor(ButtoSelected);

        RectF rect = new RectF(0, 0, Radius * 2, Radius * 2);
        RectF rectSection = new RectF(Radius - RadiusInner, Radius - RadiusInner, Radius + RadiusInner, Radius + RadiusInner);

        canvas.drawArc(rect, 0, 360, true, paintButton);
        canvas.drawArc(rectSection, 0, 360, true, paintBack);

        int TextSize = 50;
        int TextColor = Color.parseColor("#FFFFFF");
        Paint paintText = new Paint();
        paintText.setTextSize(TextSize);
        paintText.setColor(TextColor);
        paintText.setStrokeWidth(3);

        int UnitLen = 45;
        int DisToCenter = 240 - 40; // TODO CHENMY
        canvas.drawLine(Radius - DisToCenter - UnitLen, Radius, Radius - DisToCenter, Radius - UnitLen, paintText);
        canvas.drawLine(Radius - DisToCenter - UnitLen, Radius, Radius - DisToCenter, Radius + UnitLen, paintText);

        canvas.drawLine(Radius + DisToCenter + UnitLen, Radius, Radius + DisToCenter, Radius - UnitLen, paintText);
        canvas.drawLine(Radius + DisToCenter + UnitLen, Radius, Radius + DisToCenter, Radius + UnitLen, paintText);

        canvas.drawLine(Radius, Radius - DisToCenter - UnitLen, Radius - UnitLen, Radius - DisToCenter, paintText);
        canvas.drawLine(Radius, Radius - DisToCenter - UnitLen, Radius + UnitLen, Radius - DisToCenter, paintText);

        canvas.drawLine(Radius, Radius + DisToCenter + UnitLen, Radius - UnitLen, Radius + DisToCenter, paintText);
        canvas.drawLine(Radius, Radius + DisToCenter + UnitLen, Radius + UnitLen, Radius + DisToCenter, paintText);

        if (mPressed){
            int color0 = Color.WHITE;
            int color1 = Color.GRAY;
            RadialGradient radialGradient = new RadialGradient(mPressedPoint.x, mPressedPoint.y, 100, color0, color1, Shader.TileMode.CLAMP);
            Path path = new Path();
            path.addCircle(mPressedPoint.x, mPressedPoint.y, 100, Path.Direction.CCW); // 添加一个逆时针方向的圆
            Paint paint = new Paint();
            paint.setAntiAlias(true); // 抗锯齿处理
            paint.setShader(radialGradient); // 应用渐变

            canvas.drawPath(path, paint);
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventMask = event.getActionMasked();
        if (eventMask == MotionEvent.ACTION_DOWN){
            if (isPressed((int) event.getX(), (int) event.getY())){
                mPressed = true;
                mPressedPoint.set((int) event.getX(), (int) event.getY());
                mPressedTimestamp = System.currentTimeMillis();
                invalidate();
            }
        }
        else if (eventMask == MotionEvent.ACTION_MOVE){
            if (mPressed){
                mPressedPoint.set((int) event.getX(), (int) event.getY());

//                Log.i("[PRESS]", "mCurrentRank = " + mCurrentRank + " ts = " + ts + " mPressedTimestamp = " + mPressedTimestamp);
                invalidate();
            }
        }
        else if (eventMask == MotionEvent.ACTION_UP){
            mPressed = false;
            mPressedPoint.set(-1, -1);
            invalidate();
        }
        return true;
    }

    //判断点中的点在哪一个区域
    private boolean isPressed(int x, int y) {
        int xx = x - Radius;
        int yy = y - Radius;
        Log.i("[POS]", "Radius = " + Radius + "  RadiusInner = " + RadiusInner);

        Log.i("[POS]", "x = " + xx + "  y = " + yy + " fabs = " + (xx * xx + yy * yy) + " outter = " + (Radius * Radius * 4) + " inner = " + (RadiusInner * RadiusInner * 4));
        if (xx * xx + yy * yy > Radius * Radius) {
            Log.i("[POS]", "Outter");
            return false;
        }

        if (xx * xx + yy * yy < RadiusInner * RadiusInner) {
            Log.i("[POS]", "Inner");
            return false;
        }

        return true;
    }

    public void setButtonNames(String[] buttonNames){
        if (buttonNames.length != 4){
            MainApp.toast("请设置正确的摇杆按钮名称。", 2000);
            return;
        }

        ButtonNames = buttonNames;
    }
    public boolean isMotion(){
        return mPressed;
    }
    public PointF getDeriction(){

        // TODO_MOYANG
        int x = mPressedPoint.x - Radius;
        int y = mPressedPoint.y - Radius;
        double v = (double) Math.abs(x) / (double)Math.abs(y);
        double theta = Math.atan((double) Math.abs(y) / (double)Math.abs(x));
        double fScaleX = Math.cos(theta);
        double fScaleY = Math.sin(theta);
        // 左:X负值，右:X正值
        if (x < 0)
            fScaleX *= -1;
        // 上:Y负值，下:Y正值
        if (y > 0)
            fScaleY *= -1;
        Log.i("[Theta]", "v = " + v + ",theta = " + theta);
        Log.i("[Theta]", "x = " + x + ",y = " + y  + ",fScaleX = " + fScaleX + ",fScaleY = " + fScaleY);

        // return new PointF((float)fScaleX, (float)fScaleY);
        return new PointF((float)fScaleY, (float)fScaleX);
    }
    public int getRank(long currtentTimestamp){
        mCurrentRank = (int)((currtentTimestamp - mPressedTimestamp) / 500) + 1; // 2025/9/2 change from 1000 to 500 to speed up
        if (mCurrentRank > ACCELERATION_RANK)
            mCurrentRank = ACCELERATION_RANK;

        // TODO_MOYANG
        return mCurrentRank;
    }
}
